package com.example.demo.controller;

import com.example.demo.dao.Blog_article;
import com.example.demo.service.impl.Blog_articleserviceimpl;
import com.example.demo.utils.CommonRetumType;
import com.example.demo.utils.error.BusinessException;
import com.example.demo.utils.error.EmBusinessError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class Blog_articleController extends BaseController{
    @Autowired
    Blog_articleserviceimpl blog_articleserviceimpl;

    @GetMapping
    public CommonRetumType insertarticle(Blog_article blog_article) throws BusinessException {
        Integer articleinsert = blog_articleserviceimpl.insert(blog_article);
        if (articleinsert != null)
        {
            return CommonRetumType.create(articleinsert);
        }else {
            throw new BusinessException(EmBusinessError.PARAMETER_INSERT_ERROR);
        }

    }
    @GetMapping("/findbyid{id}")
    public CommonRetumType articlebyid(@RequestParam("id") Integer id) throws BusinessException {
        Blog_article blog_articlebyid = blog_articleserviceimpl.selectByPrimaryKey(id);
        if (blog_articlebyid != null)
        {
            return CommonRetumType.create(blog_articlebyid);
        }else {
            throw new BusinessException(EmBusinessError.PARAMETER_SELECT_ERROR);
        }
    }

    @PutMapping
    public CommonRetumType updatearticle(Blog_article blog_article) throws BusinessException {
        Integer articleupdate = blog_articleserviceimpl.updateByPrimaryKey(blog_article);
        if (articleupdate != null)
        {
            return CommonRetumType.create(articleupdate);
        }else {
            throw new BusinessException(EmBusinessError.PARAMETER_UPDATE_ERROR);
        }
    }

    @DeleteMapping
    public CommonRetumType  deletebyid(@RequestParam("id") Integer id) throws BusinessException {
        Integer articledelete = blog_articleserviceimpl.deleteByPrimaryKey(id);
        if (articledelete != null)
        {
            return CommonRetumType.create(articledelete);
        }else {
            throw new BusinessException(EmBusinessError.PARAMETER_DELETE_ERROR);
        }
    }
}
